//JS
layui.use(['element', 'layer', 'util', 'form'], function () {
    var element = layui.element,
        form = layui.form,
        util = layui.util,
        table = layui.table,
        $ = layui.$;

    <!-- 动态表格-学生信息 -->
    table.render({
        elem: '#teacherAssign-table',
        page: true,
        url: "/getStudentWithoutTeacher",
        cols: [
            [ //标题栏
                {type: 'checkbox'},
                {field: 'collName', title: '学院', align: 'center'},
                {field: 'majorName', title: '专业', align: 'center'},
                {field: 'sNO', title: '学生学号', width: 130, align: 'center'},
                {field: 'name', title: '学生姓名', align: 'center'},
                {title: '操作', toolbar: '#teacherAssignBtn', align: 'center'}
            ]
        ]
    });
    //监听行工具事件
    table.on('tool(teacherAssign-table)', function (obj) {
        let data = obj.data;
        if (obj.event === 'teacher-assign') {
            layer.open({
                title: '分配页面'
                , offset: 0
                , type: 1
                , shadeClose: true
                , area: ['850px', '600px']
                , success: function () {
                    $("#sno1").val(data.sNO);
                    $("#sno").html(data.sNO);
                    $("#sname1").val(data.name);
                    $("#sname").html(data.name);
                }
                , content: $('#teacherAssign-show-box')
                , end: function () {
                    $('#teacherAssign-show-box').css('display', 'none');
                }
            });
        }
    });

    <!-- 动态表格-教师选择 -->
    table.render({
        elem: '#teacherAssign-choose-table',
        page: true,
        url: "/getAllTeacher",
        cols: [
            [ //标题栏
                {field: 'tNO', title: '教师学号', width: '300', align: 'center'},
                {field: 'name', title: '教师姓名', width: '300', align: 'center'},
                {title: '操作', width: 150, toolbar: '#teacherChooseBtn', align: 'center'}
            ]
        ]
    });
    //监听行工具事件
    table.on('tool(teacherAssign-choose-table)', function (obj) {
        let data = obj.data;
        if (obj.event === 'teacher-choose') {
            layer.open({
                title: '确认分配页面'
                , offset: 0
                , type: 1
                , shadeClose: true
                , area: ['850px', '360px']
                , success: function () {
                    $("#sNO3").val(sno1.value);
                    $("#sNO2").html(sno);
                    $("#tNO3").val(data.tNO);
                    $("#tNO2").html(data.tNO);
                    $("#tName").html(data.name);
                }
                , content: $('#teacherAssign-choose-box')
                , end: function () {
                    $('#teacherAssign-choose-box').css('display', 'none');
                }
            });
        }

        form.on('submit(edit)', function (data) {

            // console.log(data.field); //当前容器的全部表单字段，名值对形式：{name: value}
            // console.log(sNO2)
            // console.log(tNO2)
            let tno = data.field.tNO2
            let sno = data.field.sNO2

            // console.log(tno)
            // console.log(typeof tno)
            // console.log(sno)
            // console.log(typeof sno)
            let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
            $.ajax({
                url: "/teacherAssign"
                , type: 'post'
                , data: {
                    tno: tno,
                    sno: sno
                }
                , dataType: 'json'
                , success: function (data) {  //调用接口成功
                    if (data === true || data === 'true') {
                        layer.close(loadPage);
                        layer.closeAll('page');
                        layui.layer.msg('修改成功！', {offset: '300px', time: 2500, anim: 0});
                        table.reload('teacherAssign-table', {});
                    } else {
                        layui.layer.close(loadPage);
                        layui.layer.alert('修改失败！');
                    }
                },
                error: function () {  //接口错误
                    layui.layer.close(loadPage);
                    layui.layer.alert('服务器出错啦,请稍后再试！');
                }
            });
            return false;//阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });

});
